package com.cap.bts.framework.common.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.httpclient.util.DateUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.cap.bts.commonfunc.common.constant.OperationType;
import com.cap.bts.commonfunc.jms.constant.HandleStatus;
import com.cap.bts.framework.bizlog.constant.StatusType;
import com.cap.bts.framework.bizlog.domain.BizLog;
import com.cap.bts.framework.bizlog.service.IBizLoggerService;
import com.cap.bts.framework.common.controller.BaseController;
import com.cap.bts.framework.common.exception.AppException;
import com.cap.bts.framework.common.util.Pager;
import com.cap.bts.framework.common.util.Utils;
import com.cap.bts.framework.message.constant.JMSConstants;
import com.cap.bts.framework.message.constant.MessageLogStatus;
import com.cap.bts.framework.message.domain.MessageLog;
import com.cap.bts.framework.message.dto.JMSLogDto;
import com.cap.bts.framework.message.service.IJmsMessageRedoService;
import com.cap.bts.framework.usermgnt.domain.User;


/**
 * Redo Message
 */
@Controller
@RequestMapping(value = "/common/message")
public class JMSRedoController extends BaseController {
	
	private static Logger logger = Logger.getLogger(JMSRedoController.class);
	

	
	
	@Autowired
	protected IBizLoggerService bizLoggerService;

    @Autowired
    IJmsMessageRedoService jmsMessageRedoService;
    
    /**
     * query  jms message from message log
     * @Date        :      Dec 8, 2011
     * @param model
     * @param queryDto
     * @return
     * @throws AppException
     */
    @RequestMapping(value = "/queryJmsRedoMessageFromMessageLog")
    public String queryJmsRedoMessageFromMessageLog(JMSLogDto queryDto,Model model) throws AppException {
        if(StringUtils.isBlank(queryDto.getOperationDateFrom())){
            queryDto.setOperationDateFrom(DateUtil.formatDate(new Date(), "yyyy-MM-dd"));
        }
        queryDto.setOperationDateTo(queryDto.getOperationDateFrom());
        queryDto.setStatusCode(MessageLogStatus.FAILURE);
        List<MessageLog> list = jmsMessageRedoService.queryNeedRedoMessage(queryDto);
        model.addAttribute("resultList", list);
        model.addAttribute("queryDto", queryDto);
        return "common/message/redo_jms_from_message_log";
    }
    /**
     * redo  jms  from message log
     * @Date        :      Dec 8, 2011
     * @param model
     * @param queryDto
     * @return
     * @throws AppException
     */
    @RequestMapping(value = "/redoJmsFromMessageLog")
    public String redoJmsFromMessageLog(JMSLogDto queryDto,Model model) throws AppException {
        if(StringUtils.isBlank(queryDto.getOperationDateFrom())){
            queryDto.setOperationDateFrom(DateUtil.formatDate(new Date(), "yyyy-MM-dd"));
        }
        queryDto.setOperationDateTo(queryDto.getOperationDateFrom());
        queryDto.setStatusCode(MessageLogStatus.FAILURE);
        queryDto.setSendReceive(JMSConstants.RECEIVE);
        List<MessageLog> results=jmsMessageRedoService.getRedoMessageByDto(queryDto);
        for (MessageLog messageLog : results) {
        	jmsMessageRedoService.redoMessageHelper(messageLog);
        }
        return queryJmsRedoMessageFromMessageLog(queryDto,model);
    }
	

    
  
}
